package org.theSeed.fileContractExt.contract.impl;

import org.redisson.api.RBitSet;
import org.redisson.api.RBucket;
import org.redisson.api.RSet;
import org.redisson.api.RedissonClient;
import org.theSeed.fileContract.contract.UploadFileRecorder;
import org.theSeed.fileContract.pojo.dto.RecordChipInfoDto;
import org.theSeed.fileContract.pojo.dto.RecordChipUploadInfoDto;
import org.theSeed.fileContract.pojo.dto.RecordFileUniInfoDto;
import org.theSeed.fileContract.utils.SeedFileConfigUtil;

import java.time.Duration;
import java.util.ArrayList;
import java.util.BitSet;
import java.util.List;
import java.util.Set;

/**
 * 十分钟内无操作，释放原有上传
 */
public class UploadFileRedisRecorder implements UploadFileRecorder {
    public UploadFileRedisRecorder(RedissonClient redissonClient) {
        this.redissonClient = redissonClient;
    }

    private RedissonClient redissonClient;

    /**
     * 分片上传初始
     */
    private String initKey = "SEED_FILE:INIT:";

    /**
     * 分片上传明细
     */
    private String chipKey = "SEED_FILE:CHIP:";

    /**
     * 缓存续期时效
     */
    private Integer expireTime = 10;

    @Override
    public void addUploadInfo(RecordFileUniInfoDto recordFileUniInfoDto) {
    }

    @Override
    public void removeUploadInfo(RecordFileUniInfoDto recordFileUniInfoDto) {

    }

    @Override
    public void addChipInfo(RecordChipInfoDto recordChipInfoDto) {
        RBucket<Integer> bucket = redissonClient.getBucket(initKey + SeedFileConfigUtil.createChipKey(recordChipInfoDto.getContractType(), recordChipInfoDto.getContractTypeInstance(), recordChipInfoDto.getResourceId()));
        RBitSet bitSet = redissonClient.getBitSet(chipKey + SeedFileConfigUtil.createChipKey(recordChipInfoDto.getContractType(), recordChipInfoDto.getContractTypeInstance(), recordChipInfoDto.getResourceId()));
        BitSet tmp = new BitSet();
        tmp.set(0,recordChipInfoDto.getChipSize(),false);
        bitSet.set(tmp);
        bucket.set(recordChipInfoDto.getChipSize(), Duration.ofMinutes(expireTime));
        bitSet.expire(Duration.ofMinutes(expireTime));
    }

    @Override
    public void addChipUploadInfo(RecordChipUploadInfoDto recordChipUploadInfoDto) {
        //初始
        RBucket<Integer> bucket = redissonClient.getBucket(initKey + SeedFileConfigUtil.createChipKey(recordChipUploadInfoDto.getContractType(), recordChipUploadInfoDto.getContractTypeInstance(), recordChipUploadInfoDto.getResourceId()));
        //上传实例
        RBitSet bitSet = redissonClient.getBitSet(chipKey + SeedFileConfigUtil.createChipKey(recordChipUploadInfoDto.getContractType(), recordChipUploadInfoDto.getContractTypeInstance(), recordChipUploadInfoDto.getResourceId()));
        //续期
        bucket.expire(Duration.ofMinutes(expireTime));

        bitSet.set(recordChipUploadInfoDto.getChipIndex(),true);
        bitSet.expire(Duration.ofMinutes(expireTime));
    }

    @Override
    public List<Integer> checkChipUploadInfo(RecordFileUniInfoDto recordFileUniInfoDto) {
        ArrayList<Integer> tmp = new ArrayList<>();
        RBitSet bitSet = redissonClient.getBitSet(chipKey + SeedFileConfigUtil.createChipKey(recordFileUniInfoDto.getContractType(), recordFileUniInfoDto.getContractTypeInstance(), recordFileUniInfoDto.getResourceId()));
        byte[] byteArray = bitSet.toByteArray();
        for (byte b : byteArray) {
            if (b == 0)
                tmp.add(1);
        }
        return tmp;
    }

    @Override
    public void removeChipUploadInfo(RecordFileUniInfoDto recordFileUniInfoDto) {
        redissonClient.getKeys()
                .delete(initKey + SeedFileConfigUtil.createChipKey(recordFileUniInfoDto.getContractType(), recordFileUniInfoDto.getContractTypeInstance(), recordFileUniInfoDto.getResourceId()),chipKey + SeedFileConfigUtil.createChipKey(recordFileUniInfoDto.getContractType(), recordFileUniInfoDto.getContractTypeInstance(), recordFileUniInfoDto.getResourceId()));
    }
}
